package top.maof.book.rank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

@Deprecated
@Component("starHotRank")
public class LikesHotRank implements HotRank, KeepRank {

    private LikesGenerator likesGenerator = new LikesGenerator();

    @Autowired
    private ZSetOperations zSetOperations;

    @Override
    public Set<Integer> dayRank(int year, int month, int week, int day, int start, int end) {
        int size = end - start;
        String key = likesGenerator.key(year, month, week, day);
        size = size > zSetOperations.size(key).intValue() ? zSetOperations.size(key).intValue() : size;
        Set<Integer> set = zSetOperations.reverseRange(key, start, size + start);
        return set;
    }

    @Override
    public Set<Integer> weekRank(int year, int month, int week, int start, int end) {
        int size = end - start;
        String key = likesGenerator.key(year, month, week);
        size = size > zSetOperations.size(key).intValue() ? zSetOperations.size(key).intValue() : size;
        Set<Integer> set = zSetOperations.reverseRange(key, start, size + start);
        return set;
    }

    @Override
    public Set<Integer> monthRank(int year, int month, int start, int end) {
        int size = end - start;
        String key = likesGenerator.key(year, month);
        size = size > zSetOperations.size(key).intValue() ? zSetOperations.size(key).intValue() : size;
        Set<Integer> set = zSetOperations.reverseRange(key, start, size + start);
        return set;
    }

    @Override
    public Set<Integer> yearWeek(int year, int start, int end) {
        int size = end - start;
        String key = likesGenerator.key(year);
        size = size > zSetOperations.size(key).intValue() ? zSetOperations.size(key).intValue() : size;
        Set<Integer> set = zSetOperations.reverseRange(key, start, size + start);
        return set;
    }

    @Override
    public Set<Integer> totalRank(int start, int end) {
        return null;
    }


    @Override
    public void add(int bookId) {
        List<String> list = likesGenerator.keys();
        list.forEach(key -> {
            zSetOperations.incrementScore(key, bookId, 1);
        });
    }

    @Override
    public void sub(int bookId) {
        List<String> list = likesGenerator.keys();
        list.forEach(key -> {
            zSetOperations.incrementScore(key, bookId, -1);
        });
    }

    public static class LikesGenerator implements Generator {

        public static final String LIKES_PREFIX = "likes:";

        @Override
        public List<String> keys() {
            Calendar calendar = Calendar.getInstance();
            List<String> list = new ArrayList<>();
            list.add(key(calendar.get(Calendar.YEAR)));
            list.add(key(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1));
            list.add(key(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                    calendar.get(Calendar.WEEK_OF_MONTH)));

            list.add(key(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                    calendar.get(Calendar.WEEK_OF_MONTH), calendar.get(Calendar.DAY_OF_MONTH)));
            return list;
        }

        @Override
        public String key(int... cycle) {
            StringBuilder sb = new StringBuilder(64);
            sb.append(Generator.PREFIX).append(LIKES_PREFIX);
            for (int i = 0; i < cycle.length; i++) {
                sb.append(cycle[i]);
                if (cycle.length - 1 > i) {
                    sb.append(":");
                }
            }
            return sb.toString();
        }
    }
}
